<?php
	require "db.php";
	$user = 'root';
	$pass = '';
	$dbh = new PDO('mysql:host=localhost;dbname=sound', $user, $pass);


$date_file = date("dmYGiv");
$name_track = $_FILES['filename']['name'];
$hash_track = addDateToFileName($name_track);

$oblojka = $_FILES['obloj']['name'];
$oblojka_track = addDateToFileName($oblojka);
$nazvanie_track = test_input($_POST["name_tr"]);
$desc_track = test_input($_POST["desc_tr"]);
$janr_track = test_input($_POST["janr_tr"]);
$autor_track = $_SESSION['logged_user']->login;
$id_autor = $_SESSION['logged_user']->id;

function addDateToFileName($file_name){
        $name = substr($file_name, 0, strrpos($file_name, '.'));  
        $extension = substr($file_name, strrpos($file_name, '.'));

        $date_file = date('dmYGiv');

        return $date_file . $extension;
    }
	
	
function test_input($data) {
		$data = trim($data);
		$data = stripslashes($data);
		$data = htmlspecialchars($data);
		return $data;
	}
	

		$sth = $dbh->prepare(
    "INSERT INTO 
                `tracks`(`oblojka_track`, `nazvanie_track`, `name_track`, `hash_track`, `desc_track`, `janr_track`, `autor_track`, `id_autor`) 
            VALUES 
                (:oblojka_track, :nazvanie_track, :name_track, :hash_track, :desc_track, :janr_track, :autor_track, :id_autor)
            ");
	$sth->execute(array('oblojka_track' => $oblojka_track,
						'nazvanie_track' => $nazvanie_track,
						'name_track' => $name_track,
						'hash_track' => $hash_track,
						'desc_track' => $desc_track,
						'janr_track' => $janr_track,
						'autor_track' => $autor_track,
						'id_autor' => $id_autor));

if(move_uploaded_file($_FILES['filename']['tmp_name'], 'music/' . $hash_track)){
	echo move_uploaded_file($_FILES['obloj']['tmp_name'], 'music/' . $oblojka_track);
	header("Location: profile.php");
} else{
	echo 'Файл НЕ на сервере';
}
?>